<?php

namespace Kunfeng\Dify\Model\File;

/**
 * 文件上传请求模型
 */
class FileUploadRequest
{
    private $file;
    private $user;
    private $fileType;
    private $fileName;
    private $fileSize;

    public function __construct($file, ?string $user = null, ?string $fileType = null, ?string $fileName = null, ?int $fileSize = null)
    {
        $this->file = $file;
        $this->user = $user;
        $this->fileType = $fileType;
        $this->fileName = $fileName;
        $this->fileSize = $fileSize;
    }

    /**
     * 创建请求
     */
    public static function create($file, ?string $user = null, ?string $fileType = null, ?string $fileName = null, ?int $fileSize = null): self
    {
        return new self($file, $user, $fileType, $fileName, $fileSize);
    }

    /**
     * 从文件路径创建请求
     */
    public static function fromPath(string $filePath, ?string $user = null): self
    {
        if (!file_exists($filePath)) {
            throw new \InvalidArgumentException("File not found: $filePath");
        }

        $fileInfo = pathinfo($filePath);
        $fileType = $fileInfo['extension'] ?? '';
        $fileName = $fileInfo['basename'] ?? '';
        $fileSize = filesize($filePath);

        return new self($filePath, $user, $fileType, $fileName, $fileSize);
    }

    /**
     * 从资源创建请求
     */
    public static function fromResource($resource, ?string $user = null, ?string $fileType = null, ?string $fileName = null, ?int $fileSize = null): self
    {
        if (!is_resource($resource)) {
            throw new \InvalidArgumentException("Invalid resource provided");
        }

        return new self($resource, $user, $fileType, $fileName, $fileSize);
    }

    // Getters
    public function getFile()
    {
        return $this->file;
    }

    public function getUser(): ?string
    {
        return $this->user;
    }

    public function getFileType(): ?string
    {
        return $this->fileType;
    }

    public function getFileName(): ?string
    {
        return $this->fileName;
    }

    public function getFileSize(): ?int
    {
        return $this->fileSize;
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'file' => $this->file,
            'user' => $this->user,
            'file_type' => $this->fileType,
            'file_name' => $this->fileName,
            'file_size' => $this->fileSize,
        ];
    }
}
